

//Fig. 28.28: DisplayQueryResults.java
//Display the contents of the Authors table in the books database.
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JTable;
import javax.swing.JOptionPane;
import javax.swing.table.TableRowSorter;
import javax.swing.table.TableModel;


public class DisplayQueryResults extends JFrame 
{
// database URL, username and password


private ResultSetTableModel tableModel;
private JTextArea queryArea;

// create ResultSetTableModel and GUI
public DisplayQueryResults(String url, String username,
		   String password, String query) 
{   
   super( "Displaying Query Results" );
     
   // create ResultSetTableModel and display database table
   try 
   {
      // create TableModel for results of query SELECT * FROM alguma coisa 
      tableModel = new ResultSetTableModel( url,username, password, query );

      // set up JTextArea in which user types queries
      queryArea = new JTextArea( query, 3, 100 );
      queryArea.setWrapStyleWord( true );
      queryArea.setLineWrap( true );
      
      JScrollPane scrollPane = new JScrollPane( queryArea,
      ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, 
      ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
      
          // create JTable based on the tableModel
      JTable resultTable = new JTable( tableModel );
       // place GUI components on content pane
      add( new JScrollPane( resultTable ), BorderLayout.CENTER );
   
      final TableRowSorter< TableModel > sorter = 
         new TableRowSorter< TableModel >( tableModel );
      resultTable.setRowSorter( sorter );
      setSize( 500, 250 ); // set window size
      setVisible( true ); // display window  
     

      
   } // end try
   catch ( SQLException sqlException ) 
   {
      JOptionPane.showMessageDialog( null, sqlException.getMessage(), 
         "Database error", JOptionPane.ERROR_MESSAGE );
            
      // ensure database connection is closed
      tableModel.disconnectFromDatabase();
      
      System.exit( 1 ); // terminate application
   } // end catch
   
   // dispose of window when user quits application (this overrides
   // the default of HIDE_ON_CLOSE)
   setDefaultCloseOperation( DISPOSE_ON_CLOSE );
   
   // ensure database connection is closed when user quits application
   addWindowListener(
   
      new WindowAdapter() 
      {
         // disconnect from database and exit when window has closed
         public void windowClosed( WindowEvent event )
         {
            tableModel.disconnectFromDatabase();
            System.exit( 0 );
         } // end method windowClosed
      } // end WindowAdapter inner class
   ); // end call to addWindowListener
} // end DisplayQueryResults constructor

// execute application
public static void main( String args[] ) 
{  

   new DisplayQueryResults("jdbc:mysql://localhost/sisgel","admin","admin","SELECT * FROM usuario");
 
   
} // end main
} // end class DisplayQueryResults

